﻿#include "InputNodeDialog.h"
#include "ui_InputNodeDialog.h"

#include <QDebug>

#ifdef Q_OS_LINUX
#include "../../sdk/include/IDevFunction.h"
#endif

InputNodeDialog::InputNodeDialog(QWidget *parent) :
    BaseDialog(parent, QStringLiteral("输入节点配置")),
    ui(new Ui::InputNodeWidget)
{
    resize(800, 600);

    QWidget* contentWidget = new QWidget(this);
    ui->setupUi(contentWidget);

    setContentWidget(contentWidget);

    // 设置数量
    ui->tableWidget->setRowCount(5);
    ui->tableWidget->setColumnCount(5);
    // 设置表头
    QStringList headerLabels
    {
        QStringLiteral("编号"),
        QStringLiteral("状态"),
        QStringLiteral("设备名称"),
        QStringLiteral("IP地址/域名"),
        QStringLiteral("快捷键设置"),
    };
    ui->tableWidget->setHorizontalHeaderLabels(headerLabels);
    auto* horizontalHeader = ui->tableWidget->horizontalHeader();
    // 列的宽度随内容自适应
    horizontalHeader->setSectionResizeMode(QHeaderView::ResizeToContents);
    // 名称列和IP地址列的宽度设为自动扩增
    horizontalHeader->setSectionResizeMode(2, QHeaderView::Stretch);
    horizontalHeader->setSectionResizeMode(3, QHeaderView::Stretch);
    //设置列间距
    horizontalHeader->setDefaultSectionSize(20);
    // 不要高亮，不然会出现很丑的高亮阴影
    horizontalHeader->setHighlightSections(false);
    // 去除单元格选中后出现的虚线框
    ui->tableWidget->setFocusPolicy(Qt::NoFocus);

    // 开始设置表格内容
    QList<QStringList> content
    {
        { QStringLiteral(""), "192.168.123.82", "Ctrl+1" },
        { QStringLiteral("苹果123"), "192.168.123.85", "Ctrl+2" },
        { QStringLiteral("贺蒙的电脑"), "192.168.123.79", "Ctrl+3" },
        { QStringLiteral("来"), "192.168.123.78", "Ctrl+4" },
        { QStringLiteral("财务电脑"), "192.168.123.76", "Ctrl+5" },
    };

    int i = 0;
    for (auto& row : content)
    {
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(
                                     QString("%1").arg(i+1, 2, 10, QLatin1Char('0'))));
        QLabel* statusLable = new QLabel();
        // 这里需要做个兼容，因为每个操作系统下的风格不一样，导致大小有差异
#ifdef Q_OS_LINUX
        QSize iconSize(17, 17);
#else
        QSize iconSize(24, 24);
#endif
        statusLable->setPixmap(QPixmap("://assets/green-ball.png")
                               .scaled(iconSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
        statusLable->setAlignment(Qt::AlignCenter);
        ui->tableWidget->setCellWidget(i, 1, statusLable);

        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(row[0]));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(row[1]));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(row[2]));

        ++i;
    }

    // 设置所有单元格文本居中显示
    for(int i = 0; i < ui->tableWidget->rowCount(); ++i)
    {
        for(int j = 0; j < ui->tableWidget->columnCount(); ++j)
        {
            auto* item = ui->tableWidget->item(i,j);
            if(item)
            {
                item->setTextAlignment(Qt::AlignCenter);
            }
        }
    }

#ifdef Q_OS_LINUX
//    Function* function = new Function();
//    if (function)
//    {
//        QString json("{ \"Id\": 125345, \"Name\": \"NetNvr\" }");
//        function->Create("192.168.123.102", 60008);
//        qDebug() << "jsonX:" << function->GetConfig(json.toStdString().c_str());
//        delete function;
//    }
#endif
}

InputNodeDialog::~InputNodeDialog()
{
    delete ui;
}
